﻿/**********************************************
 * 功能名称：资源动因自动处理
编写人：  杨莹
建立时间： 杨 2009年8月18日
说明：
根据企业自定义的动因量执 行规则。填充动因量。
 

 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Text;
using System.Collections.Generic;


namespace Hisun.View.CostDriver
{
    public partial class AutoFullCostDriver : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();

        public string alert;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
//污水动因量的填充
        protected void Button_Save_Click(object sender, EventArgs e)
        {
            try
            {
                db.污水动因处理(Application["accountPeriod"].ToString());
                alert = "<script>alert('数据写入成功');</script>";
            }
            catch 
            {
                alert = "<script>alert('数据写入失败');</script>";
            }
            
        }
//燃动的填充
        protected void Button_Save_Randong_Click(object sender, EventArgs e)
        {
            string MyAp = Application["accountPeriod"].ToString();

            try
            {
                db.燃动动因处理(MyAp);


                var MyPowerIndexCenters = from a in db.TABC_DM_PowerIndex
                                          select new
                                          {
                                              a.作业中心ID,
                                              a.成本细项ID,
                                              a.系数
                                          };
                foreach (var MyPowerIndexCenter in MyPowerIndexCenters)
                {
                    var PowerIndex = MyPowerIndexCenter.系数;
                    var CostItems = MyPowerIndexCenter.成本细项ID;
                    var CenterID = MyPowerIndexCenter.作业中心ID;


                    var MyCenterDriverList = from a in db.TABC_JS_CostCenterPathDriver
                                             join b in db.TABC_JS_CostCenter on a.作业中心ID equals b.作业中心ID
                                             where a.目标中心ID == CenterID
                                             && a.会计期间ID == MyAp
                                             && b.成本细项ID == CostItems
                                             select a;


                    ChildCenterList MyChildCenterList1 = new ChildCenterList(CenterID);
                    var  MyChildCenterList2 =  (from a in MyChildCenterList1.CostCenterlist select a.作业中心ID).ToList();

                    try
                    {
                        var MyMount = (from c in db.V作业中心批时汇总数
                                       where c.会计期间ID == MyAp
                                       && MyChildCenterList2.Contains(c.作业中心ID)
                                       select c.批时).Sum();

                        decimal xx = (decimal)MyMount * (decimal)PowerIndex;

                        foreach (var MyCenterDriver in MyCenterDriverList)
                        {

                            if(MyMount == 0)
                            {
                              MyCenterDriver.动因量 = 0;
                              db.SubmitChanges();
                            }

                            if (PowerIndex == 0)
                            {
                               
                            }
                            if (xx > 0)
                            { 
                                MyCenterDriver.动因量 = xx;
                                db.SubmitChanges();
                            }

                           


                        }
                    }
                    catch
                    {
                        foreach (var MyCenterDriver in MyCenterDriverList)
                        {


                         //   MyCenterDriver.动因量 = 0;

                         //   db.SubmitChanges();


                        }
                    
                    }


                   

                }
                alert = "<script>alert('数据写入成功');</script>";
                
            }
            catch
            {

                alert = "<script>alert('数据写入失败');</script>";
            
            }


        }
    }
}
